package com.zhdl.modules.demo.DCHTProcess.camera;

import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
import com.zhdl.common.ytools.callback.CommonCallback;
import com.zhdl.hardware_contact.hardware_config.entity.ProjectConfiguration;
import com.zhdl.hardware_contact.hardware_web.service.HardwareContactService;
import com.zhdl.modules.process.common.GetPLCValue;
import com.zhdl.modules.process.dto.MyEx;
import com.zhdl.network.strategy.common_server.impl.CommonServerSendServiceImpl;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Slf4j
@Service
public class CameraProcessService implements GetPLCValue {

    @Resource
    HardwareContactService hardwareContactService;

    @Resource
    CommonServerSendServiceImpl commonServerSendService;

    @Resource
    S7PLC s7PLC;

    @Resource
    CameraInterface cameraInterface;

    //RAM中的参数列表
    public static List<ProjectConfiguration> projectConfigurations;

    //相机读取出来的PLC值
    public String cameraValue = "Heart, 2";

    //相机读取是否完成
    private boolean cameraReadComplete = false;

    //读取到的数据回调
    private List<CommonCallback<List<ProjectConfiguration>>> changeCallBacks = new ArrayList<>();

    //错误信息回调
    private List<CommonCallback<List<MyEx>>> errCallBacks = new ArrayList<>();

    //历史配置信息 Map
    Map<String, String> lastValue = new HashMap<>();

    //Camera端口配置
    public String cameraPort = "2000";


    /**
     * 初始化项目的配置信息
     */
    public void initCameraData(String projectName) {
        // 初始化相机配置
        projectConfigurations = hardwareContactService.initProjectConfiguration(projectName);

        // 判断相机服务端是否开启
        if (!commonServerSendService.serverClientList.keySet().isEmpty()) {
            for (String s : commonServerSendService.serverClientList.keySet()) {
                if (s.split(":")[1].equals(cameraPort)) {
                    // 数据回调
                    commonServerSendService.setDataCallBack(s, s1 -> {
                        log.info(s);
                        readProjectConfigToRAM(s1);
                    });
                }
            }
        }
    }

    /**
     * 修改相机回调的数据
     */
    private void notifyCallbacks(List<ProjectConfiguration> projectParmaList) {
        if (!changeCallBacks.isEmpty()) {
            for (CommonCallback<List<ProjectConfiguration>> changeCallBack : changeCallBacks) {
                changeCallBack.statusCallback(projectParmaList);
            }
        }
    }

    /**
     * 解析PLC值 TO RAM
     *
     * @param readValue plc的值
     */
    public void readProjectConfigToRAM(String readValue) {
        //获取PLCValue
        cameraValue = readValue;

        //初始化一个异常列表
        List<MyEx> exList = new ArrayList<>();
        if (projectConfigurations == null || projectConfigurations.isEmpty()) {
            return;
        }

        String[] cameraValues = cameraValue.split(",");

        //配置列表 Key为filed value为value
        for (ProjectConfiguration projectConfiguration : projectConfigurations) {
            switch (projectConfiguration.getField()){
                case "camera_type" ->{
                    projectConfiguration.setValue(cameraValues[0]);
                }
                case "camera_cmdNumber" ->{
                    projectConfiguration.setValue(cameraValues[1]);
                }
                case "camera_cmdResult" ->{
                    projectConfiguration.setValue(cameraValues[2]);
                }
                case "camera_Result" ->{
                    if (cameraValues[0].equals("T102")) {
                        if (cameraValues[3].equals("ok")){
                            projectConfiguration.setValue(cameraValues[3]);
                            break;
                        }
                    }
                    projectConfiguration.setValue("");
                }

            }
            if (projectConfiguration.getField().equals("DB1")) {
                lastValue.put(projectConfiguration.getField(), projectConfiguration.getValue());
            }
        }

        //更新回调信息
        notifyCallbacks(projectConfigurations);

        /*
            第一次读取保存到数据库信息
         */
        if (!cameraReadComplete) {
            cameraReadComplete = true;
            projectConfigurations.forEach(projectConfiguration -> {
                hardwareContactService.saveProjectConfig(projectConfiguration);
            });
        }
    }

    @Override
    public ProjectConfiguration getValue(String field) {
        for (ProjectConfiguration configurationPLc : cameraInterface.configurationPLcList) {
            if (configurationPLc.getField().equals(field)) {
                return configurationPLc;
            }
        }
        return null;
    }

}
